package br.inf.ufrgs.usixml4desktop.rendering.javadesktop.components;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.Box;
import javax.swing.JComponent;

import org.jdom.Element;

import br.inf.ufrgs.usixml4desktop.exceptions.ParsingErrorException;
import br.inf.ufrgs.usixml4desktop.exceptions.RenderingErrorException;
import br.inf.ufrgs.usixml4desktop.rendering.UsiXMLInterfaceRenderer;
import br.inf.ufrgs.usixml4desktop.rendering.UsiXMLLayoutManagerRenderer;
import br.inf.ufrgs.usixml4desktopl.rendering.javadesktop.RendererFactory;
import br.inf.ufrgs.usixml4desktopl.rendering.javadesktop.UsiXMLJavaDesktopInterfaceRenderer;
import br.inf.ufrgs.usixml4desktop.rendering.javadesktop.utils.InformationParser;

public class UsiXMLJavaDesktopFlowBoxRenderer extends UsiXMLLayoutManagerRenderer {

	public UsiXMLJavaDesktopFlowBoxRenderer(UsiXMLJavaDesktopInterfaceRenderer usiXMLInterfaceRenderer) {
		super(usiXMLInterfaceRenderer);
	}

	@Override
	public UsiXMLLayoutManagerRenderer getNextRenderer() {
		return null;
	}

	@Override
	public String getValidElementName() {
		return "flowBox";
	}

	@Override
	public Container processRenderization(Element element, Container contentPane) 
		throws ParsingErrorException,RenderingErrorException {
		
		FlowLayout flow = new FlowLayout();
		InformationParser infoParser = new InformationParser();
		
		String alignmentString = element.getAttributeValue("alignment");
		int alignment = infoParser.parseFlowLayoutAlignment(alignmentString);
		flow.setAlignment(alignment);
		
		contentPane.setLayout(flow);
                
		for (Object obj : element.getChildren()) {
			Element child = (Element) obj;
			
		JComponent component = (JComponent) 
			RendererFactory.getInstance().
				getFirstElementRenderer((UsiXMLJavaDesktopInterfaceRenderer)this.usiXMLInterfaceRenderer).renderElement(child);
		
		contentPane.add(component);
		}
		
		return contentPane;
		
	}
	
	@Override
	public boolean validateElement(Element element) {
		return true;
	}
}
